import { login, syncMiniUserInfo } from '../../api/index'
Component({

	/**
	 * 页面的初始数据
	 */
	data: {
		userInfo: {},
		avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
	},
	methods: {
		getuserinfo(e) {
			const that = this
			wx.showLoading({title: '登录中'})
			console.log(e)
			that.setData({
				userInfo: e.detail.userInfo
			})
			wx.getUserProfile({
				desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success: (res) => {
					// that.setData({
					// 	userInfo: res.userInfo
					// })
					console.log(res.userInfo)
					// that.login()
				},
				fail:(err) => {
					console.log(err)
					wx.hideLoading()
				}
			})
		},
		login() {
			const that = this
			wx.login({
				success (res) {
					if (res.code) {
						login({ code: res.code }).then(res => {
							wx.setStorageSync('token', res.data.token)
							syncMiniUserInfo({ avatarUrl: that.data.userInfo.avatarUrl, nickName: that.data.userInfo.nickName }).then(res => {
								var myEventDetail = {} // detail对象，提供给事件监听函数
								var myEventOption = {} // 触发事件的选项
								that.triggerEvent('ok', myEventDetail, myEventOption)
								wx.hideLoading()
							}).catch(err => {
								wx.hideLoading()
								wx.showToast({
									title: err.errorMsg,
									icon: 'none',
								})
							})
						}).catch(err => {
							wx.hideLoading()
							wx.showToast({
								title: err.errorMsg,
								icon: 'none',
							})
						})
					} else {
						console.log(err)
						wx.hideLoading()
					}
				},
				fail (err) {
					console.log(err)
					wx.hideLoading()
				}
			})
		},
		onChooseAvatar(e) {
			const { avatarUrl } = e.detail 
			console.log(e)
			this.setData({
				avatarUrl,
			})
		},
		changeg(e) {
			console.log(e)
		},
		dcc (e) {
			console.log(e)
		},
	}
})